<template>
  <div class="sign-up" @click='$refs.verify.show = false'>
    <div style="height:50px;"></div>
    <div class="logo-box">
      <img
        width="150"
        :src="$store.state.logoImg"
        @click="$router.push('/')"
      />
      <div>{{ i18nText('login.signUp', '注册') }}</div>
    </div>
    <div class="login-container">
      <!-- 注册 -->
      <Form
        ref="formRegist"
        :model="formRegist"
        :rules="ruleInline"
        style="width:400px;"
      >

        <Row>
          <Col span="12">
            <FormItem prop="firstName">
              <i-input
                type="text"
                v-model="formRegist.firstName"
                clearable
                :placeholder="i18nText('personInfo.firstName', '名字')"
              >
                <Icon type="md-person" slot="prepend"></Icon>
              </i-input>
            </FormItem>
          </Col>
          <Col span="12">
            <FormItem prop="lastName">
              <i-input
                type="text"
                v-model="formRegist.lastName"
                clearable
                :placeholder="i18nText('personInfo.lastName', '姓氏')"
              >
              </i-input>
            </FormItem>
          </Col>
        </Row>
        <FormItem prop="email">
          <i-input
            type="text"
            v-model="formRegist.email"
            clearable
            :placeholder="i18nText('personInfo.emailAddress', '邮箱')"
          >
            <Icon type="md-mail" slot="prepend"></Icon>
          </i-input>
        </FormItem>
        <FormItem prop="code">
          <i-input
            type="text"
            v-model="formRegist.code"
            clearable
            :placeholder="i18nText('login.verificationCode', '邮箱验证码')"
          >
            <Icon
              type="ios-text-outline"
              style="font-weight: bold"
              slot="prepend"
            />
            <Button slot="append" @click="sendCode">{{ codeMsg }}</Button>
          </i-input>
        </FormItem>
        <FormItem>
          <Button @click="verifyBtnClick" long :type="verifyStatus?'success':'default'">
            {{ verifyStatus ? i18nText('login.verificationPassed', '验证通过') : i18nText('login.clickToSubmitVerification', '点击完成安全验证') }}
          </Button>
        </FormItem>
        <FormItem>
          <Button type="error" size="large" @click="handleRegist" long>
            {{ i18nText('login.createAccount', '注册') }}
          </Button>
        </FormItem>
        <FormItem>
<!--          <span class="color999 ml_20">点击注册，表示您同意《<router-link to="/article?id=1371992704333905920" target="_blank">商城用户协议</router-link>》</span>-->
          {{ i18nText('login.agreeToTheAgreement', '点击注册，表示您同意《商城用户协议》') }}
        </FormItem>
      </Form>
      <!-- 拼图验证码 -->
      <Verify
        ref="verify"
        class="verify-con"
        :verifyType="verifyType"
        @change="verifyChange"
      ></Verify>
      <div class="login-btn">
        {{ i18nText('login.alreadyHaveAccount', '已有账号？') }}
        <a @click="$router.push('login')">{{ i18nText('login.signIn', '立即登录') }}</a>
      </div>
    </div>
    <!--    <div class="foot">
          <Row type="flex" justify="space-around" class="help">
            <router-link to="/article" class="item" target="_blank">帮助</router-link>
            <router-link to="/article?id=1371779927900160000" class="item" target="_blank">隐私</router-link>
            <router-link to="/article?id=1371992704333905920" class="item" target="_blank">条款</router-link>
          </Row>
          <Row type="flex" justify="center" class="copyright">
            Copyright © {{year}} - Present
            <a href="https://pickmall.cn" target="_blank" style="margin: 0 5px">{{config.title}}</a>
            版权所有
          </Row>
        </div>-->
  </div>
</template>

<script>

import * as RegExp from '@/plugins/RegExp.js';
import {md5} from '@/plugins/md5.js';
import * as apiLogin from '@/api/login.js';
import {sendEmailCode} from '@/api/common.js';
import Verify from '@/components/verify';
import { i18nText } from '@/utils/i18n'

export default {
  name: 'SignUp',
  components: {Verify},
  data() {
    return {
      config: require('@/config'),
      year: new Date().getFullYear(),
      formRegist: {
        // 注册表单
        email: '',
        code: '',
        username: '',
        password: '',
        firstName: '',
        lastName: '',
      },
      ruleInline: {
        // 验证规则
        firstName: [{required: true, message: i18nText('personInfo.pleaseEnterFirstName', '请输入名字')}],
        lastName: [{required: true, message: i18nText('personInfo.pleaseEnterLastName', '请输入姓氏')}],
        password: [
          {required: true, message: i18nText('accountSecurity.pleaseEnterPassword', '请输入密码')},
          {type: 'string', min: 6, message: i18nText('accountSecurity.passwordLengthLimit', '密码不能少于6位')}
        ],
        mobilePhone: [
          {required: true, message: i18nText('personInfo.pleaseEnterPhoneNumber', '请输入手机号码')},
          {
            pattern: RegExp.mobile,
            trigger: 'blur',
            message: i18nText('personInfo.pleaseEnterCorrectPhoneNumber', '请输入正确的手机号')
          }
        ],
        email: [
          {required: true, message: i18nText('login.enterEmailAddress', '请输入邮箱')},
          {
            pattern: RegExp.email,
            message: i18nText('login.pleaseEnterTheCorrectEmailAddress', '请输入正确的邮箱'),
          },
        ],
        code: [{required: true, message: i18nText('login.enterEmailVerificationCode', '请输入邮箱验证码')}]
      },
      verifyStatus: false, // 是否验证通过
      verifyType: 'REGISTER', // 验证状态
      codeMsg: i18nText('login.send', '发送验证码'), // 提示文字
      interval: '', // 定时器
      time: 60 // 倒计时
    };
  },
  created() {
  },
  methods: {
    // 注册
    handleRegist() {
      this.$refs.formRegist.validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.formRegist));
          // 拼接用户名
          data.username = this.formRegist.firstName.trim() + ' ' + this.formRegist.lastName.trim()
          data.password = md5(data.password);
          apiLogin.registerByEmail(data).then((res) => {
            if (res.success) {
              this.$Message.success(i18nText('login.signUpSuccess', '注册成功!'));
              this.$router.push('login');
            } else {
              this.$Message.warning(res.message);
            }
          });
        } else {
        }
      });
    },
    // 发送短信验证码
    sendCode() {
      if (this.time === 60) {
        if (this.formRegist.email === '') {
          this.$Message.warning(i18nText('login.pleaseFillEmailAddressFirst', '请先填写邮箱'));
          return;
        }
        if (!this.verifyStatus) {
          this.$Message.warning(i18nText('login.pleaseCompleteSecurityVerificationFirst', '请先完成安全验证'));
          return;
        }
        let params = {
          email: this.formRegist.email,
          verificationEnums: 'REGISTER'
        };
        sendEmailCode(params).then(res => {
          if (res.success) {
            this.$Message.success(i18nText('login.verificationCodeSentSuccessfully', '验证码发送成功'));
            let that = this;
            this.interval = setInterval(() => {
              that.time--;
              if (that.time === 0) {
                that.time = 60;
                that.codeMsg = i18nText('login.resend', '重新发送');
                that.verifyStatus = false;
                clearInterval(that.interval);
              } else {
                that.codeMsg = that.time;
              }
            }, 1000);
          } else {
            this.$Message.warning(res.message);
          }
        });
      }
    },
    // 图片验证码成功回调
    verifyChange(con) {
      if (!con.status) return;
      this.$refs.verify.show = false;
      this.verifyStatus = true;
    },
    // 打开图片验证码
    verifyBtnClick() {
      if (!this.verifyStatus) {
        this.$refs.verify.init();
      }
    }
  },
  mounted() {
    this.$refs.formRegist.resetFields();
    document.querySelector('.sign-up').style.height = window.innerHeight + 'px'
  }
};
</script>
<style scoped lang="scss">
.sign-up {
  height: 100%;
}
.logo-box {
  width: 600px;
  height: 80px;
  margin: 0 auto;
  display: flex;
  align-items: center;

  img {
    width: 150px;
    cursor: pointer;
  }

  div {
    font-size: 20px;
    margin-top: 10px;
  }
}

.login-container {
  border-top: 2px solid $theme_color;
  position: relative;
  margin: 0 auto;
  width: 700px;
  background-color: #fff;
  padding: 20px 150px;

  .login-btn {
    position: absolute;
    right: 20px;
    top: -45px;
  }

  /deep/ input {
    padding: 18px 5px;
  }
}

.verify-con {
  position: absolute;
  left: 140px;
  top: 80px;
  z-index: 10;
}

.other-login {
  margin: 0 auto;

  .ivu-icon {
    font-size: 24px;
  }
}

.regist {
  display: flex;
  justify-content: flex-end;
  margin-top: -10px;

  span {
    margin-left: 10px;

    &:hover {
      cursor: pointer;
      color: $theme_color;
    }
  }
}

.foot {
  position: fixed;
  bottom: 4vh;
  width: 368px;
  left: calc(50% - 184px);
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;

  .help {
    margin: 0 auto;
    margin-bottom: 1vh;
    width: 60%;

    .item {
      color: rgba(0, 0, 0, 0.45);
    }

    :hover {
      color: rgba(0, 0, 0, 0.65);
    }
  }
}
</style>
